package com.westvalley.syn.service;

import java.util.UUID;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.westvalley.common.kit.SqlsKit;
import com.westvalley.common.kit.StringKit;
import com.westvalley.common.kit.SqlsKit.SqlOperateType;
import com.westvalley.common.result.SqlResult;
import com.westvalley.service.cache.NcDeptCache;
import com.westvalley.syn.bean.Result;

/**
 * NC部门
 * @author 莫双
 *
 */
public class NcDeptService extends NcService{
	private static final String KEY = "nc_dept";
	private NcDeptCache ncDeptCache = NcDeptCache.me();
	
	public NcDeptService(){
		refreshCache();
		this.url = prop.getPropValue(KEY);
	}
	
	@Override
	protected Result synMainData(JSONArray jsonArrData) {
		SqlResult sqlResultInsert = SqlResult.me();
		SqlResult sqlResultUpdate = SqlResult.me();
		for(int i=0;i<jsonArrData.size();i++){
			JSONObject ob = jsonArrData.getJSONObject(i);
			if( ob!=null ){
				String pk_deptdoc = ob.getString("pk_deptdoc");	//部门主键
				if( StrKit.notBlank(pk_deptdoc) ){
					Record ncUnitRecord = this.ncDeptCache.getRecordByCode(pk_deptdoc);	//缓存中信息
					if(ncUnitRecord==null){
						sqlResultInsert.set(insertData(pk_deptdoc,ob));
					}else{
						sqlResultUpdate.set(updateData(pk_deptdoc,ob));
					}
				}else{
					return Result.me(new RuntimeException("数据主键为空！"));
				}
			}else{
				return Result.me(new RuntimeException("数据解析失败！"));
			}
		}
		int[] insertNums = sqlResultInsert.batch(500);
		int[] updateNums = sqlResultUpdate.batch(500);
		return Result.me(jsonArrData.size(),insertNums,updateNums);
	}

	@Override
	protected Result synpOtherData(Result result, JSONArray jsonArrData) {
		return result;
	}

	@Override
	protected void refreshCache() {
		this.ncDeptCache.reload();
	}
	
	/**
	 * 新增数据
	 * @param pk
	 * @param ob
	 * @return
	 */
	private SqlsKit insertData(String pk,JSONObject ob){
		SqlsKit sqlsKit = SqlsKit.me();
		sqlsKit.clear();
		sqlsKit.setTableName("WV_NC_DEPT");
		sqlsKit.addData("id", UUID.randomUUID().toString());
		sqlsKit.addData("deptcode", StringKit.null2Str(ob.getString("deptcode")));
		sqlsKit.addData("deptname", StringKit.null2Str(ob.getString("deptname")));
		sqlsKit.addData("pk_deptdoc", StringKit.null2Str(ob.getString("pk_deptdoc")));
		sqlsKit.addData("pk_fathedept", StringKit.null2Str(ob.getString("pk_fathedept")));
		sqlsKit.addData("unitcode", StringKit.null2Str(ob.getString("unitcode")));
		sqlsKit.setOperateType(SqlOperateType.INSERT);
		return sqlsKit;
	}
	
	/**
	 * 更新数据
	 * @param pk
	 * @param ob
	 * @return
	 */
	private SqlsKit updateData(String pk,JSONObject ob){
		SqlsKit sqlsKit = SqlsKit.me();
		sqlsKit.clear();
		sqlsKit.setTableName("WV_NC_DEPT");
		sqlsKit.addData("deptcode", StringKit.null2Str(ob.getString("deptcode")));
		sqlsKit.addData("deptname", StringKit.null2Str(ob.getString("deptname")));
		sqlsKit.addData("pk_fathedept", StringKit.null2Str(ob.getString("pk_fathedept")));
		sqlsKit.addData("unitcode", StringKit.null2Str(ob.getString("unitcode")));
		sqlsKit.setWhere("pk_deptdoc", StringKit.null2Str(ob.getString("pk_deptdoc")));
		sqlsKit.setOperateType(SqlOperateType.UPDATE);
		return sqlsKit;
	}

}
